<?php if (!defined("_ECRIRE_INC_VERSION")) return;

/**
 * Dynamic tag that outputs quantity of product that belongs to composite with taking 
 * to account user changed value 
 */

function balise_ECATALOGUE_COMPOSITE_QUANTITY($p) {	
	return calculer_balise_dynamique($p,'ECATALOGUE_COMPOSITE_QUANTITY', array());
}

function balise_ECATALOGUE_COMPOSITE_QUANTITY_stat($args, $filtres) {    
	return $args;
}

function balise_ECATALOGUE_COMPOSITE_QUANTITY_dyn($id_composite, $id_article) {
    if (!($id_composite = intval($id_composite)) || !($id_article = intval($id_article))) return false;
	include_spip('functions/pdk_common.php');
    $cart = pdk_load_class('Alohacart','classes');
	
    // If some exceptions are thrown - return empty value 
    try {
        // If user changed number of this product, we will use this value
        if (!($quantity = $cart->GetCompositeProduct($id_composite, $id_article))) {
            //var_Dump($quantity);
            $composite_article_mdl = pdk_load_class('CompositeArticleModel','classes');
            $row = $composite_article_mdl->select("id_composite=".$id_composite." AND id_article=".$id_article);
            if (isset($row[0]) && isset($row[0]["quantity"]))
                $quantity = $row[0]["quantity"];
            else
                $quantity = ''; 
        }
    } catch (Exception $e) {
        $quantity = '';
    } 
    
    //$product = $cart->GetProduct($id_composite);
    //if ($product)
      //  $quantity = $quantity * $product;
    
    return array('balise/ecatalogue_composite_quantity', 0, 
		array('quantity' => $quantity            
    ));
}

?>